#include "plan.h"

Plan::Plan() : SolidObject(), normal()
{

}

Plan::~Plan()
{

}

Plan::Plan(const Plan &plan) : SolidObject(plan), normal(plan.normal)
{

}

Plan& Plan::operator=(const Plan &plan)
{
    ((SolidObject*)this)->operator=(plan);
    normal = plan.normal;
    return (*this);
}

Plan::Plan(const Point3D &_position, const Vector3D &_normal, const Color &_material) : SolidObject(_position,_material), normal(_normal)
{

}

inline void Plan::texturing(const Intersection &inter)
{
    Vector3D planToPoint(inter - (*this));
    double X(planToPoint*XVec), Y(planToPoint*YVec);
    texture->getColorAt(X,Y,material);
}

inline void Plan::intersection(const Ray &ray,Intersection &inter) const
{
    //Vector3D oc((Point3D)(*this) - ray.getOrigin());
    double scalaire;
    scalaire=ray.getDirection()*normal;
    if( scalaire < 0.0)
    {
        double a,b,c,k;
        Vector3D v(getX(),getY(),getZ());
        Vector3D vO(ray.getOrigin().getX(),ray.getOrigin().getY(),ray.getOrigin().getZ());
        a=normal*v;
        b=normal*vO;
        c=normal*ray.getDirection();
        k=(a-b)/c;
        if( k > 0.0 )
        {
            inter.setDistance(k);
            inter.setPosition(ray.getOrigin()+ ray.getDirection()*k);
            inter.setValid(true);
            inter.setObject((SolidObject*)this);
        }
        else
            inter.setValid(false);
    }
    else
        inter.setValid(false);
}


inline void Plan::getNormalVectorAt(const Point3D &, Vector3D &n) const
{
    n=normal;
}
